package com.splic.api.controller.product;

import com.github.pagehelper.PageInfo;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.ShiroUtils;
import com.splic.product.domain.UserMotionData;
import com.splic.product.dto.UserMotionRankingDto;
import com.splic.product.service.UserMotionDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户运动数据
 */
@RestController
@RequestMapping("/api/motion")
@Slf4j
public class ApiUserMotionController extends BaseController {

    @Autowired
    private UserMotionDataService userMotionDataServiceImpl;
    /**
     * 保存用户运动量
     * @return
     */
    @PostMapping("/saveUserMotion")
    public ResponseObj saveUserMotion(@RequestBody UserMotionData userMotionData) {
        try {
            if(userMotionData.getWalkStep() == null || userMotionData.getHeightRise() == null){
                return ResponseObj.ERROR("参数为空");
            }
            userMotionData.setUserId(ShiroUtils.getUserId());
            int count = userMotionDataServiceImpl.insert(userMotionData);
            if(count <= 0){
                throw new BusinessException("用户运动量保存失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("用户运动量保存失败:{}",e);
            return ResponseObj.ERROR("用户运动量保存失败");
        }
    }

    /**
     * 获取用户运动量列表
     * @return
     */
    @GetMapping("/getUserMotionList")
    public ResponseObj<PageInfo<UserMotionData>> getUserMotionList() {
        try{
            startPage();
            UserMotionData userMotionData = new UserMotionData();
            userMotionData.setUserId(ShiroUtils.getUserId());
            List<UserMotionData> list = userMotionDataServiceImpl.selectUserMotionDataList(userMotionData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户设置数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户设置数据列表失败");
        }
    }


    /**
     * 获取运动量排名接口
     * @return
     */
    @GetMapping("/getMotionRankingList")
    public ResponseObj<PageInfo<UserMotionRankingDto>> getMotionRankingList(String rankingType) {
        try{
            startPage();
            List<UserMotionRankingDto> list = userMotionDataServiceImpl.selectUserMotionRankingList(rankingType,ShiroUtils.getUserId());
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户设置数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户设置数据列表失败");
        }
    }
}
